package com.jack.thinking.java._22.swing.pane;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import com.utils.SwingConsole;

/**
 * 弹出式菜单, 在window 右键菜单
 * @author wangbingwu
 *
 */
public class Popup extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPopupMenu popup = new JPopupMenu();
	private JTextField t = new JTextField(10);
	
	public Popup() {
		setLayout(new FlowLayout());
		add(t);
		ActionListener al = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				t.setText(((JMenuItem)e.getSource()).getText());
			}
		};
		
		JMenuItem m = new JMenuItem("Hither");
		m.addActionListener(al);
		popup.add(m);
		m = new JMenuItem("Yon");
		m.addActionListener(al);
		popup.add(m);
		m = new JMenuItem("Afar");
		m.addActionListener(al);
		popup.add(m);
		PopupListener pl = new PopupListener();
		addMouseListener(pl);
		t.addMouseListener(pl);
	}
	
	class PopupListener extends MouseAdapter{
		@Override
		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}
		
		@Override
		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);
		}
		
		private void maybeShowPopup(MouseEvent e) {
			if(e.isPopupTrigger()) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new Popup(), 300, 200);
	}
}
